package com.jiuyuejiu.utils;


import android.content.Context;
import android.os.Environment;

import com.jiuyuejiu.application.ConstValues;
import com.jiuyuejiu.application.MyApplication;
import com.jiuyuejiu.func_video.domain.VideoFenleiStc;
import com.jiuyuejiu.func_video.domain.VideoDetailStc;
import com.kylin.core.app.Latte;
import com.kylin.core.utils.dbtutil.PrefUtils;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * UI工具类
 */
public class ShanHaiUtil {

    private static int picCount = ConstValues.videoUrlList.length;

    /**
     * 读取文件内容
     *
     * @param
     * @return
     */
    public static String parseFileToString(File docFile) throws Exception {

        int length = (int) docFile.length();
        byte[] buff = new byte[length];
        FileInputStream fin = new FileInputStream(docFile);
        fin.read(buff);
        fin.close();
        String result = new String(buff, "GB2312");
        if (result != null) {
            // 判断是否是乱码
            if (!(java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(result))) {
                try {
                    result = new String(buff, "UTF-8");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return result;
    }

    /**
     * 将字符串分割成数组
     *
     * @param result
     * @return
     */
    public static String[] splitStringToArray(String result) {

        String[] read;
        if (result.contains("\r\n")) {
            read = result.split("\r\n");
        } else if (result.contains("\r")) {
            read = result.split("\r");
        } else {
            read = result.split("\n");
        }
        return read;
    }

    /**
     * 将分类数组 转成分类集合
     *
     * @param readResult
     * @return
     */
    public static ArrayList<VideoFenleiStc> parseStringArrayToList(String[] readResult) {

        Random rand = new Random();

        ArrayList<VideoFenleiStc> videoFenleiStcs = new ArrayList<>();

        VideoFenleiStc stc;

        for (int i = 0; i < readResult.length; i++) {
            String s = readResult[i];
            // 分类
            stc = new VideoFenleiStc();
            if (!(s.contains("_list") || s.contains("_LIST") || s.contains("双击提取文件夹中文件名") || s.contains("新建文件夹"))) {
                stc.setCategory(s);// 分类名称
                stc.setImageurl(ConstValues.videoUrlList[rand.nextInt(picCount)]);// 分类封面
                videoFenleiStcs.add(stc);
            }
        }
        return videoFenleiStcs;
    }


    /**
     * 将字符串数组转成视频对象集合
     *
     * @param readResult
     * @return
     */
    public static ArrayList<VideoDetailStc> getVideoListByVideoArray( String categoryname, String filepath, String[] readResult) {

        Random rand = new Random();

        ArrayList<VideoDetailStc> videoDetailStcs = new ArrayList<>();

        VideoDetailStc stc;
        for (int i = 0; i < readResult.length; i++) {
            String s = readResult[i];
            stc = new VideoDetailStc();
            if (!(s.contains("_list") || s.contains("_LIST") || s.contains("双击提取文件夹中文件名") || s.contains("新建文件夹"))) {
                stc.setCategory(categoryname);// 视频类别
                stc.setImageurl(ConstValues.videoUrlList[rand.nextInt(picCount)]);// 视频封面
                stc.setVideopath(filepath + categoryname + "/");// 视频中间路径 "landking/video/06 出差同房篇/"
                stc.setVideoname(s);// 视频名称
                stc.setVideourl(getHttpid() + filepath + categoryname + "/" + s);// 视频链接
                videoDetailStcs.add(stc);
            }
        }
        return videoDetailStcs;
    }


    /**
     * 拼接文件路径
     *
     * @param filename  _LIST.TXT
     * @return 文件路径
     */
    public static String parseFilePath(String filename) {


        // String sPath = MyApplication.getAppContext().getFilesDir().getAbsolutePath()+ "/";
        String sdcardPath = Environment.getExternalStorageDirectory().getPath() + "/";

        // /data/user/0/packname/files/123456localpath/filename/
        // String filepath = sPath + ConstValues.LOCALPATH + "/" + filename;

        // /storage/emulated/0/123456localpath/filename/
        String filepath = sdcardPath + ConstValues.LOCALPATH + "/" + filename;

        return filepath;
    }

    // 获取服务器ip
    public static  String getHttpid(){
        // 获取链接的头部 默认PC
        return PrefUtils.getString(Latte.getApplicationContext(), ConstValues.HTTPSERVICE, ConstValues.HTTPHEAD_PC);
    }

}
